<template>
  <div>
    <div class="top-box">
      <router-link to="/admin/news/dc/detail"><el-button type="success">添加资料</el-button></router-link>
    </div>
    <loading v-if="loading"></loading>
    <div class="pd" v-else>
      <div class="search-box">
        <el-input v-model="searchParam.id" placeholder="请输入编号" class="user-input"></el-input>
			<el-input v-model="searchParam.title" placeholder="请输入资料标题" class="user-input"></el-input>
			<el-input v-model="searchParam.access_key" placeholder="请输入access_key" class="user-input"></el-input>
        <el-button type="primary" class="el-icon-search" @click="search">搜索</el-button>
        <el-button class="el-icon-q" @click="cancel">撤销</el-button>
      </div>

      <el-table :data="listData.list" border fit highlight-current-row style="width:100%">
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column label="封面图">
          <template scope="scope">
            <img :src="scope.row.img" class="art-cover" @click="previewImg(scope.row.img)">
          </template>
        </el-table-column>
        <el-table-column label="资料标题" prop="title"></el-table-column>
        <el-table-column label="资料简介" prop="note"></el-table-column>
        <el-table-column label="下载是否要登陆" prop="need_auth_desc"></el-table-column>
        <!--<el-table-column label="下载次数" prop="down_count"></el-table-column>-->
        <el-table-column label="access_key" prop="access_key"></el-table-column>
        <el-table-column label="发布时间" prop="repub_time"></el-table-column>
        <el-table-column label="是否推荐">
          <template scope="scope">
            <div class="input-group-box">
              <el-switch
                v-model="scope.row.is_recommend"
                on-text="是"
                off-text="否"
                @change="changeRecommend($event, scope.row.id, scope.row.title, scope.$index)">
              </el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <i class="i-icon icon-revamp" title="修改"  @click="edit(scope.row)"></i>
            <i class="i-icon icon-delete" title="删除"  @click="remove(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="listData.total">
        </el-pagination>
      </div>
      <el-dialog
        title="查看封面图"
        :visible.sync="imgVisible"
        :before-close="handleClose"
        size="tiny">
        <img :src="imgUrl" style="width:100%">
      </el-dialog>
    </div>
  </div>
</template>
<script>
import apiConfig from '../../utils/apiconfig'
import api from '../../utils/api'
import helper from '../../utils/helper'
import loading from '../../components/loading'
export default {
  data () {
    return {
      listData: [],
      pageSize: 20,
      pageall: 1,
      page: 1,
      loading: true,
      showAddModelFlag: false,
      showUpdateModelFlag: false,
      imgVisible: false,
      imgUrl: '',
      searchParam: {
        id: '',
        title: '',
        access_key: ''
      }
    }
  },
  created () {
    this.getList()
  },
  components: {
    loading
  },
  methods: {
    getSearchParam () { // 参数
      var param = {
        page: this.page,
        id: this.searchParam.id,
        title: this.searchParam.title,
        access_key: this.searchParam.access_key
      }
      this.params = param
    },
    getList () {
      this.getSearchParam()
      api.GET(helper.getManageApiUrl(apiConfig.downloadCenterList), this.params, res => {
        this.listData = res
        var list = this.listData.list
        list.forEach((item, index) => {
          if (item.is_recommend === 0) {
            item.is_recommend = false
          } else {
            item.is_recommend = true
          }
        })
        this.loading = false
      }, res => {
        console.log(res)
      })
    },
    search () {
      this.getList()
    },
    cancel () {
      this.searchParam.title = ''
      this.searchParam.note = ''
      this.searchParam.access_key = ''
    },
    handleChange (val) {
      this.page = val
      this.getList()
    },
    remove (id) {
      var param = {
        id: id
      }
      this.$confirm('确认要删除此条信息吗？')
      .then(_ => {
        api.GET(helper.getManageApiUrl(apiConfig.deleteDownloadCenter), param, res => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 1500
          })
          this.getList()
        }, res => {
          this.$notify({
            title: '失败',
            message: '删除失败',
            type: 'error',
            duration: 1500
          })
        })
      })
      .catch(_ => {})
    },
    edit (item) {
      let url = '/admin/news/dc/detail?id=' + item.id
      this.$router.push(url)
    },
    changeRecommend (event, id, title, index) {
      var tip = event === true ? '确定设置资料“' + title + '”为推荐吗' : '确定取消新闻“' + title + '”的推荐吗？'
      var param = {
        id: id,
        is_recommend: event === true ? 1 : 0
      }
      this.changeStatus(param, tip, () => {
        this.listData.list[index].is_recommend = !event
      })
    },
    changeStatus (param, tip, fn) {
      this.$confirm(tip)
        .then(_ => {
          api.GET(helper.getManageApiUrl(apiConfig.updateDownloadCenterStatus), param, res => {
            this.$notify({
              title: '成功',
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getlist()
          }, res => {
            this.$notify({
              title: '失败',
              message: res.msg,
              type: 'error',
              duration: 1500
            })
          })
        }).catch(_ => {
        typeof fn === 'function' && fn()
      })
    },
    previewImg (cover) {
      this.imgUrl = cover
      this.imgVisible = true
    },
  }
}
</script>
<style>
 .search-box {
  font-size: 0;
  padding-bottom:10px;
 }
 .search-box .user-input{
   width: 197px;
   margin: 3px 2px;
 }
 .art-cover {
   width: 95px;
   height: 70px;
 }
 .art-fade {
   position: fixed;
   top: 0;
   left: 0;
   width: 100%;
   height: 100%;
   z-index: 99;
   background: rgba(255, 255, 255, 0.7);
   padding-top: 5%;
 }
</style>

